import { _decorator, Component, Node, input, Input, Vec3, EventTouch, Prefab, instantiate, Collider2D, Contact2DType, IPhysics2DContact, Label } from 'cc';
const { ccclass, property } = _decorator;
import {eventTarget, GameEvent} from './EventSystem'

@ccclass('PlayerControl')
export class PlayerControl extends Component {

    @property(Prefab)
    playerBullet_prefab: Prefab;
    @property(Node)
    bulletParent: Node = null;
    @property(Node)
    bulletPosition: Node = null;
    @property
    shootRate: number = 0.3;
    @property
    public hp: number = 3;
    @property(Label)
    PlayerHp: Label = null;

    
    collider: Collider2D = null;
    shootTimer: number = 0;

    //在血量变化时派发事件
    private updateHpUI(){
        this.PlayerHp.string = this.hp.toString();
        //派发血量变化事件,为了GameManger脚本能够通过监听事件获取玩家当前血量状态而进行逻辑判断
        eventTarget.emit(GameEvent.PLAYER_HP_CHANGED, this.hp);
        if(this.hp <= 0){
            eventTarget.emit(GameEvent.PLAYER_DIED);
        }
    }

    protected onLoad(): void {
        //监听屏幕上所有触摸
        input.on(Input.EventType.TOUCH_MOVE, this.touchMove, this);
        //设置血量UI初始化
        this.PlayerHp.string = this.hp.toString();  //记得类型转换

    }
    protected onDestroy(): void {
        //玩家节点被摧毁时，结束监听
        input.off(Input.EventType.TOUCH_MOVE, this.touchMove, this);
        this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }
    //给回调函数touchMOve()添加EventTouch类型的参数可以获取触摸偏移量的数据
    touchMove(event: EventTouch) {
        //初始位置
        const p = this.node.position;
        //目标位置
        let targetPosition = new Vec3(p.x + event.getDeltaX(), p.y + event.getDeltaY(), p.z);
        //判断屏幕边界，防止飞机移动出屏幕边缘
        if (targetPosition.x < -200) {
            targetPosition.x = -200;
        }
        if (targetPosition.x > 200) {
            targetPosition.x = 200;
        }
        if (targetPosition.y < -450) {
            targetPosition.y = -450;
        }
        if (targetPosition.y > 450) {
            targetPosition.y = 450;
        }
        //移动玩家飞机节点
        this.node.setPosition(targetPosition);
    }
    start() {       
        this.collider = this.getComponent(Collider2D);
        if (this.collider) {
            this.collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){
        this.hp -= 1;
        //发生碰撞时及时更新血量UI显示
        this.updateHpUI();
        if(this.hp <= 0){
            if(this.collider){
                this.collider.enabled = false;
            }
            this.PlayerHp.string = "0";

            setTimeout(()=>{
                this.node.destroy();
            },0);
        }
    }

    update(deltaTime: number) {
        this.shootTimer += deltaTime;
        if (this.shootTimer >= this.shootRate) {
            this.shootTimer = 0;
            this.createOneBullet();
        }        
    }

    createOneBullet() {
        //判断要获取的子弹预制体是否为空
        if (this.playerBullet_prefab == null) return;
        //从Prefab中实例化出新节点，bulletPrefab
        const bulletPrefab = instantiate(this.playerBullet_prefab);
        //把新节点设置成自定义的子弹父节点里。
        this.bulletParent.addChild(bulletPrefab);
        //设置新节点bulletPrefab的位置，即设置子弹生成以后的位置,用自定义好的子弹生成位置节点的世界坐标
        //注意设置子弹位置时要和预设的子弹生成位置同步世界坐标，所以要用setWorldPosition()
        bulletPrefab.setWorldPosition(this.bulletPosition.worldPosition);
    }
}


